﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;

using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmWKParameter : BaseQryRpt  
    {
        public enum FromType
        {
            SetBOM =0 ,
            Product,
            ProductBOMRev,
            ProductBOMSales,
        }
        
        private struct OrigConValue
        {
            public Control con ;
            public string strOrigValue ;
            public string strFieldName;
        }

        private string mstrTableName = string.Empty  ;
        private FromType mFromType = new FromType();
        private string mStrRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess();
        private List<Control> mControls = new List<Control> { };  
        private List<OrigConValue> mOrigConValue  = new List < OrigConValue >{};
        private string GetTableName ()
        {
            if (mFromType == FromType.Product)
            {
                return "rmProduct";
            }
            else if (mFromType == FromType.ProductBOMRev)
            {
                return "rmProductBOMRev";
            }
            else if (mFromType == FromType.SetBOM)
            {
                return "rmCategorySetBOMHdr";
            }
            else if (mFromType == FromType.ProductBOMSales)
            {
                return "rmProductBOMSales";
            }
            else
                return "";
        }
        public frmWKParameter()
        {
            InitializeComponent();        
        }

        public frmWKParameter(FromType FromType)
        {
            InitializeComponent();     
            mFromType = FromType;
        }

        public frmWKParameter(string strRecID)
        {
            InitializeComponent();
            mStrRecID = strRecID;
        }

        public bool FillContent()
        {
            return FillContent(mStrRecID);
        }

        private void LoadControlList()
        {
            mOrigConValue.Clear();
            mControls.Clear();
            OrigConValue OrigCon = new OrigConValue();
            foreach ( Control con in grpBaseInfo.Controls)
            {
                mControls.Add(con);
                OrigCon.strOrigValue = con.Text;
                OrigCon.strFieldName = con.Name.Substring(3, con.Name.Length - 3);
                OrigCon.con  = con;
                mOrigConValue.Add(OrigCon);
            }

            foreach (Control con in grp1.Controls)
            {
                mControls.Add(con);
                OrigCon.strOrigValue = con.Text;
                OrigCon.strFieldName = con.Name.Substring(3, con.Name.Length - 3);
                OrigCon.con  = con;
                mOrigConValue.Add(OrigCon);
            }
            foreach (Control con in grp2.Controls)
            {
                mControls.Add(con);                
                OrigCon.strOrigValue = con.Text;
                OrigCon.strFieldName = con.Name.Substring(3, con.Name.Length - 3);
                OrigCon.con = con;
                mOrigConValue.Add(OrigCon);

            }
            foreach (Control con in grp3.Controls)
            {
                mControls.Add(con);              
                OrigCon.strOrigValue = con.Text;
                OrigCon.strFieldName = con.Name.Substring(3, con.Name.Length - 3);
                OrigCon.con = con;
                mOrigConValue.Add(OrigCon);

            }
        }



        public bool FillContent( string strRecID)
        {
            if (GetTableName() == "rmProductBOMRev")
            {
                if (CvtFunc.ToBoolean(DataFunctions.GetFieldValue("rmProductBOMRev", "VerClosed", "RecID='" + mStrRecID + "'")))
                {
                    this.Enabled = false;
                }
                else
                {
                    this.Enabled = true;
                }
            }
            else if (GetTableName() == "rmProductBOMSales")
            {
                 if (CvtFunc.ToBoolean(DataFunctions.GetFieldValue("rmProductBOMSales", "VerClosed", "RecID='" + mStrRecID + "'")))
                {
                    this.Enabled = false;
                }
                else
                {
                    this.Enabled = true;
                }
            }
            LoadControlList();
            string strFieldName = string.Empty;
            string strSQL = "select * from " + GetTableName() + " where RecID='" + strRecID + "'";
            DBAccess dbcon = new DBAccess(false, false);
            DataTable dtCon = dbcon.GetDataTable(strSQL);
            dbcon.CloseCon();
            if (dtCon.Rows.Count > 0)
            {
                foreach (DataRow drCon in dtCon.Rows)
                {
                    foreach (Control con in this.mControls)
                    {
                        foreach (DataColumn col in drCon.Table.Columns)
                        {
                            if (con.Name.Length > 3)
                            {
                                if (con.Name.ToUpper().Trim().Substring(3, con.Name.Length - 3) == col.ToString().Trim().ToUpper())
                                {
                                    con.Text = CvtFunc.ToString(drCon[col]);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (Control con in this.mControls)
                {
                    if(  con.GetType().ToString () == "System.Windows.Forms.TextBox" )
                    con.Text = "0.00";
                }
                
            }
            mStrRecID = strRecID;
            return true ;
        }

        public bool  ConfirmChange()
        {
            foreach( OrigConValue origCon in mOrigConValue )
            {
                if ( origCon.con.Text != origCon.strOrigValue )
                {
                    return true ;
                    break ;
                }
            }
            return false ;
        }

        public bool UpdateContent()
        {
            if (this.ConfirmChange())
            {
                string strSQL = string.Empty;
                List<string> strFields = new List<string> { "LabelUnitQty",	"LabelLength",	"LabelWidth" ,	"PrintLength",
	                        "PrintWidth" ,	"LaminatingLength" ,	"LaminatingWidth" ,	"CutFactor" ,
	                        "PrintUseQty", 	"ContainQty" ,	"PackageQty" ,	"InnerDiameterLength",
	                        "InnerDiameterWidth" ,	"InnerDiameterHeight",	"InnerOutsideDiff" ,	"MiddleWidth",	
                            "BurrWidth" ,	"OtherPrintWidth" ,	"OtherPrintBackWidth" ,	"UseIronNailQty" ,	
                            "LockingWireQty" ,	"LockingWireLength","LabelSize","Quantity","SetSize","ProductLength",
                            "ProductWidth","PaperHeight","Pages","PagePrintWidth","PagePrintLength","ProdDiameter"};
                if (ConfirmChange())
                {
                    int i = 0;
                    string strTmpSQL = "";
                    DBAccess dbcon = new DBAccess(false, false);
                    strSQL = " select * from " + GetTableName() + " where RecID='" + mStrRecID + "'";
                    DataTable dtTable = dbcon.GetDataTable( strSQL ); 
                    foreach (OrigConValue con in mOrigConValue)
                    {
                        string strType = "";
                        if (strFields.Contains(con.strFieldName))
                        {
                            strType = dtTable.Columns[con.strFieldName].DataType.Name;
                            if (strType == "Int32")
                            {
                                strTmpSQL += " " + con.strFieldName + " ='" +  CvtFunc.ToInt ( con.con.Text) + "',";
                            }
                            else
                            {
                                strTmpSQL += " " + con.strFieldName + " ='" +   con.con.Text + "',";
                            }
                            i++;
                        }
                    }
                    strTmpSQL = strTmpSQL.Substring(0, strTmpSQL.Length - 1);
                    strSQL = " update " + GetTableName() + " set " + strTmpSQL + " where RecID='" + mStrRecID + "'";                 
                    dbcon.ExecuteSql(strSQL);
                    dbcon.CloseCon();
                }             
            }
            return true;
        }
    }
}
